package com.iflysse.controller;

import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.req.KnowledgePointBoReq;
import com.iflysse.pojo.bo.resp.KnowledgePointBoResp;
import com.iflysse.pojo.bo.resp.KnowledgePointListBoResp;
import com.iflysse.pojo.vo.req.KnowledgePointVoReq;
import com.iflysse.pojo.vo.resp.KnowLedgePointResp;
import com.iflysse.pojo.vo.resp.KnowledgePointVoResp;
import com.iflysse.services.KnowledgePointService;
import com.iflysse.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/knowLedgePoint")
@Api(value = "老师端知识点的接口")
@Log4j
public class KnowLedgePointController {


    @Autowired
    private KnowledgePointService knowledgePointService;

    @PostMapping("/queryAllKnowLedgePoint")
    @ApiOperation(value = "查询知识点")
    public KnowledgePointVoResp queryAllKnowLedgePoint(KnowledgePointVoReq knowledgePointVoReq){
        KnowledgePointVoResp knowledgePointVoResp=new KnowledgePointVoResp();

        try {

           log.info("查询知识点的参数bean:"+knowledgePointVoReq);
            KnowledgePointBoReq knowledgePointBoReq=   CommonUtil.objToObj(knowledgePointVoReq,KnowledgePointBoReq.class);
            knowledgePointBoReq.setKnowledgePointId(knowledgePointVoReq.getKnowledgeParentId());
            log.info("教师端查询知识点的参数bean:"+knowledgePointBoReq);
            KnowledgePointListBoResp knowledgePointListBoResp= knowledgePointService.getKnowledgePointListByParentId(knowledgePointBoReq);
            log.info("教师端查询知识点的响应bean:"+knowledgePointListBoResp);
            List<KnowledgePointBoResp> knowledgePointBoRespList=knowledgePointListBoResp.getKnowledgePointBoRespList();

            List<KnowLedgePointResp>result=new ArrayList<KnowLedgePointResp>();
            knowledgePointBoRespList.forEach(knowledgePointBoResp -> {
                KnowLedgePointResp knowLedgePointResp=new KnowLedgePointResp();
                knowLedgePointResp.setKnowLedgePointId(knowledgePointBoResp.getKnowledgePointId());//知识点编号，也是下一节知识点的父编号
                knowLedgePointResp.setKnowLedgePointName(knowledgePointBoResp.getKnowledgePointName());//知识点名称
                result.add(knowLedgePointResp);
            });
            knowledgePointVoResp.setResult(result);
            knowledgePointVoResp.setCode(ResultEnum.SUCCESS.getCode());
            knowledgePointVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("教师端查询知识点列表响应bean:"+knowledgePointVoResp);

            return knowledgePointVoResp;
        }catch (Exception e){
            log.error("教师端查询知识点列表出现异常，信息为："+e);
            CommonUtil.printError(knowledgePointVoResp);
            return knowledgePointVoResp;
        }
    }
}
